import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Main {

	Database db;

	/**
	 * 抗生素
	 * */
	private static List<String> mycinList_chname = null;
	private static Map<String, String> mycinMap_ecname = null;

	/**
	 * 各种菌
	 * */
	private static List<String> microList = null;

	/**
	 * 农业
	 * */
	private static List<String> agriList_chname = null;
	private static Map<String, String> agriMap_ecname = null;
	
	/**
	 * 农业菌
	 * */
	private static List<String> agristrainsList_chname = null;
	private static Map<String, String> agristrainsMap_ecname = null;
	
	/**
	 * 农业作用、动作之类
	 * */
	private static List<String> agtieffectList = null;
	

	/**
	 * 初始化
	 * */
	@SuppressWarnings("unchecked")
	void init() {
		db = new Database();

		mycinList_chname = db.getMycinChname();
		mycinMap_ecname = db.getMycin();

		microList = db.getMycrobeChname();

		agriList_chname = db.getAgriChname();
		agriMap_ecname = db.getAgri();
		
		agristrainsList_chname = db.getChname("agriculture_strains", "chname");
		agristrainsMap_ecname = db.getEnCnMap("agriculture_strains", "enname", "chname");
		
		agtieffectList = db.getChname("argriculture_effects", "effect");

		Collections.sort(mycinList_chname, new StringComarator());
		Collections.sort(microList, new StringComarator());
		Collections.sort(agriList_chname, new StringComarator());
		Collections.sort(agristrainsList_chname, new StringComarator());
		Collections.sort(agtieffectList, new StringComarator());

		// for (String s : agristrainsList_chname) { System.out.println(s); }

		// ResultSet wanfangResult = db.getWanfangDetail(0, 100);

		// db.addMark(567,"awfwaerrg","说的话有人","uhyu","afwef","额也让");
	}

	void exec_patent() {

		init();

		int step = 1000; // 每次处理这么多条记录

		int count = 0; // 当前已处理的记录条数

		ResultSet rs = null;

		Database dbInsert = new Database(); // 专门用于插入的db

		boolean isFinished = false; // 是否已处理了所有记录
		
		List<String> fieldsList = new ArrayList<String>(); // 记录所要查询的字段
		fieldsList.add("id");
		fieldsList.add("patent_name");
		fieldsList.add("abstract");
		fieldsList.add("sovereignty");	

		while (!isFinished) {
				
			rs = db.getTableDetail("patent_detail", fieldsList, count, step);

			try {
				while (rs.next()) {

					int id = rs.getInt("id");

					String mycin_title = getMark_list(mycinList_chname, rs.getString("patent_name"));

					String mycin_abstract = getMark_list(mycinList_chname, rs.getString("abstract")+rs.getString("sovereignty"));

					String microbe_title = getMark_list(microList, rs.getString("patent_name"));

					String microbe_abstract = getMark_list(microList, rs.getString("abstract")+rs.getString("sovereignty"));

					String agir_title = getMark_list(agriList_chname, rs.getString("patent_name"));

					String agri_abstract = getMark_list(agriList_chname, rs.getString("abstract")+rs.getString("sovereignty"));
							
					String agristrains_title = getMark_list(agristrainsList_chname, rs.getString("patent_name"));

					String agristrains_abstract = getMark_list(agristrainsList_chname, rs.getString("abstract")+rs.getString("sovereignty"));

					String agtieffect_title = getMark_list(agtieffectList, rs.getString("patent_name"));

					String agtieffect_abstract = getMark_list(agtieffectList, rs.getString("abstract")+rs.getString("sovereignty"));
					
					
					Map<String, String> all = new LinkedHashMap<String, String>();
					all.put("mycin_title", mycin_title);
					all.put("mycin_abstract", mycin_abstract);
					all.put("microbe_title", microbe_title);
					all.put("microbe_abstract", microbe_abstract);
					all.put("agir_title", agir_title);
					all.put("agri_abstract", agri_abstract);
					all.put("agristrains_title", agristrains_title);
					all.put("agristrains_abstract", agristrains_abstract);
					all.put("agtieffect_title", agtieffect_title);
					all.put("agtieffect_abstract", agtieffect_abstract);
					
					if(judgeMap(all)){
						dbInsert.addMark("mark_patent", id, all);
					}
					

					count++;
					System.out.println("第" + count + "条已完成");
				}

			} catch (SQLException e) {
				System.out.println("第" + count + "条记录有问题");
				e.printStackTrace();
			}

			System.out.println("已经完成" + count + "条记录");

			if (count % step != 0) {
				isFinished = true;
			}

		}

	}
	
	void exec() {

		init();

		int step = 1000; // 每次处理这么多条记录

		int count = 0; // 当前已处理的记录条数

		ResultSet rs = null;

		Database dbInsert = new Database(); // 专门用于插入的db

		boolean isFinished = false; // 是否已处理了所有记录

		while (!isFinished) {
			rs = db.getWanfangDetail(count, step);

			try {
				while (rs.next()) {

					/*
					 * int id = rs.getInt("id"); String mycin_title =
					 * preProcess(rs.getString("title_cn")); String
					 * mycin_abstract = preProcess(rs.getString("title_en"));
					 * String microbe = preProcess(rs.getString("keyword_cn"));
					 * String agir_title =
					 * preProcess(rs.getString("keyword_cn")); String
					 * agri_abstract = preProcess(rs.getString("keyword_cn"));
					 * agir_title = "阿"; agri_abstract =
					 * "5%%阿维''菌素\\\'微乳\\\"剂防治水稻稻纵卷叶螟田间药效试验报告";
					 */

					int id = rs.getInt("id");

					String mycin_title = getMark_list(mycinList_chname, rs.getString("title_cn") + rs.getString("keyword_cn"));
					if (mycin_title.equals("")) {
						mycin_title = getMark_map(mycinMap_ecname, rs.getString("title_en") + rs.getString("title_cn"));
					}

					String mycin_abstract = getMark_list(mycinList_chname, rs.getString("abstract_cn"));
					if (mycin_abstract.equals("")) {
						mycin_abstract = getMark_map(mycinMap_ecname, rs.getString("abstract_cn"));
					}

					String microbe_title = getMark_list(microList, rs.getString("title_cn") + rs.getString("keyword_cn"));

					String microbe_abstract = getMark_list(microList, rs.getString("abstract_cn"));

					String agir_title = getMark_list(agriList_chname, rs.getString("title_cn") + rs.getString("keyword_cn"));
					if (agir_title.equals("")) {
						agir_title = getMark_map(agriMap_ecname, rs.getString("title_en") + rs.getString("title_cn"));
					}

					String agri_abstract = getMark_list(agriList_chname, rs.getString("abstract_cn"));
					if (agri_abstract.equals("")) {
						agri_abstract = getMark_map(agriMap_ecname, rs.getString("abstract_cn"));
					}
					
					String agristrains_title = getTitleMark(agristrainsList_chname, agristrainsMap_ecname, rs);
					String agristrains_abstract = getAbstractMark(agristrainsList_chname, agristrainsMap_ecname, rs);
					String agtieffect_title = getTitleMark(agtieffectList, rs);
					String agtieffect_abstract = getAbstractMark(agtieffectList, rs);
					
					Map<String, String> all = new LinkedHashMap<String, String>();
					all.put("mycin_title", mycin_title);
					all.put("mycin_abstract", mycin_abstract);
					all.put("microbe_title", microbe_title);
					all.put("microbe_abstract", microbe_abstract);
					all.put("agir_title", agir_title);
					all.put("agri_abstract", agri_abstract);
					all.put("agristrains_title", agristrains_title);
					all.put("agristrains_abstract", agristrains_abstract);
					all.put("agtieffect_title", agtieffect_title);
					all.put("agtieffect_abstract", agtieffect_abstract);
					
					if(judgeMap(all)){
						dbInsert.addMark("mark2", id, all);
					}
					

					/*if (!mycin_title.equals("") || !mycin_abstract.equals("") || !microbe_title.equals("") || !microbe_abstract.equals("")
							|| !agir_title.equals("") || !agri_abstract.equals("")) {
						dbInsert.addMark(id, mycin_title, mycin_abstract, microbe_title, microbe_abstract, agir_title, agri_abstract);
					}*/
					
					

					count++;
					System.out.println("第" + count + "条已完成");
				}

			} catch (SQLException e) {
				System.out.println("第" + count + "条记录有问题");
				e.printStackTrace();
			}

			System.out.println("已经完成" + count + "条记录");

			if (count % step != 0) {
				isFinished = true;
			}

		}

	}

	private String preProcess(String s) {
		if (s == null)
			return "";
		return s.replace("'", "\\\'").replace("\"", "\\\"").replace("%", "%%").trim();
	}
	
	/**
	 * 判断这个map中的value值有没有不是空串的。若有至少一个value不是空串，返回true；否则返回false。
	 * */
	private boolean judgeMap(Map<String, String> map){
		boolean result = false;
		for (String key : map.keySet()) {
			if(!map.get(key).equals("")){
				result = true;
			}
		}
		return result;
	}
	
	private String getTitleMark(List<String> list, ResultSet rs){
		String result = "";
		try {
			result = getMark_list(list, rs.getString("title_cn") + rs.getString("keyword_cn"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	private String getTitleMark(List<String> list, Map<String, String> map, ResultSet rs){
		String result = "";
		try {
			result = getMark_list(list, rs.getString("title_cn") + rs.getString("keyword_cn"));
			if (result.equals("")) {
				result = getMark_map(map, rs.getString("title_en") + rs.getString("title_cn"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	private String getAbstractMark(List<String> list, ResultSet rs){
		String result = "";
		try {
			result = getMark_list(list, rs.getString("abstract_cn"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	private String getAbstractMark(List<String> list, Map<String, String> map, ResultSet rs){
		String result = "";
		try {
			result = getMark_list(list, rs.getString("abstract_cn"));
			if (result.equals("")) {
				result = getMark_map(map, rs.getString("abstract_cn"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 判断字符串s中是否包含list中的字符串。若包含，则返回所有被包含的字符串，用“;”分隔；否则返回空字符串。
	 * */
	private String getMark_list(List<String> list, String s) {
		if (s == null || s.trim().equals("")) {
			return "";
		}
		s = s.trim();
		String result = "";
		boolean isFirst = true;
		for (String cur : list) {
			if (s.contains(cur)) {
				if (!result.contains(cur)) { // 应对“青霉素”和“天青霉素”这种情况
					if (!isFirst)
						result += ";";
					result += cur;
					isFirst = false;
				}
			}
		}
		result = preProcess(result);
		return result;
	}

	/**
	 * 判断字符串s中是否包含map的key中的字符串（英文）。若包含，则返回map中相应的value；否则返回空字符串。
	 * */
	private String getMark_map(Map<String, String> map, String s) {
		if (s == null || s.trim().equals("")) {
			return "";
		}
		s = s.trim();

		String result = "";
		boolean isFirst = true;
		for (String key : map.keySet()) {
			if (s.contains(key)) {
				if (!isFirst)
					result += ";";
				result += map.get(key);
				isFirst = false;

			}
		}
		result = preProcess(result);
		return result;
	}

	public static void main(String[] args) {

		new Main().exec_patent();

		/*
		 * Database db = new Database(); List<String> chname =
		 * db.getMycinChname(); Map<String,String> ecname = db.getMycin();
		 * 
		 * for(String s : chname){ //System.out.println(s); }
		 * 
		 * for(String key : ecname.keySet()){
		 * System.out.println(key+"      "+ecname.get(key)); }
		 * db.closeConnection();
		 */
	}

}

/**
 * 比较器，用于降序排列
 * */
class StringComarator implements Comparator {
	@Override
	public int compare(Object o1, Object o2) {
		String s1 = (String) o1;
		String s2 = (String) o2;

		if (s1.length() < s2.length()) {
			return 1;
		}
		if (s1.length() > s2.length()) {
			return -1;
		}
		return 0;
	}

}
